using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Tomahawk.Runtime.Screen.Controls;

namespace Tomahawk.Runtime.Screen
{
    /// <summary>
    /// This class manages the components and controls that make possible the Graphics user interface of a game.
    /// </summary>
    public class ScreenManager
    {
        public event Engine.LoadDelegate OnLoadTick;

        private SpriteBatch spriteBatch;
        private GraphicsDevice graphicsDevice;
        private ContentManager contentManager;
        private List<Form> forms;

        /// <summary>
        /// Constructor
        /// </summary>
        public ScreenManager()
        {
            this.forms = new List<Form>();
        }

        /// <summary>
        /// Load the contents on all the nodes
        /// </summary>
        internal void LoadGraphicsContent(GraphicsDevice graphicsDevice, ContentManager contentManager)
        {
            //store for loading stuff ahead of time
            this.graphicsDevice = graphicsDevice;
            this.contentManager = contentManager;

            //create the sprite batch for rendering
            spriteBatch = new SpriteBatch(graphicsDevice);

            //load the contents
            foreach (Form frm in this.forms)
            {
                frm.LoadGraphicsContent(graphicsDevice, contentManager);
                if (this.OnLoadTick != null) this.OnLoadTick();
            }

        }

        /// <summary>
        /// Render all nodes
        /// </summary>
        internal void Render(GraphicsDevice device)
        {
            //render the forms
            foreach (Form frm in this.forms)
            {
                frm.Render(device, spriteBatch);
            }

        }

        /// <summary>
        /// Update the screen control system
        /// </summary>
        internal void Update(float elapsedTime)
        {
            //render the forms

                foreach (Form frm in this.forms)
                {
                    frm.Update(elapsedTime);
                }


        }

        /// <summary>
        /// Add a form to the collection
        /// </summary>
        /// <param name="frm"></param>
        public void AddForm(Form frm)
        {
            if (frm != null)
                this.forms.Add(frm);
        }

        /// <summary>
        /// Remove a form from the collection
        /// </summary>
        /// <param name="frm"></param>
        public void RemoveForm(Form frm)
        {
            if (frm != null)
                this.forms.Remove(frm);
        }

    }

}


